package com.cl.mypureframe.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.cl.mypureframe.R;
import com.cl.mypureframe.fragment.FourFragment;
import com.cl.mypureframe.fragment.HomeFragment;
import com.cl.mypureframe.fragment.ThreeFragment;
import com.cl.mypureframe.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @Class: ViewPagerFragmentActivity
 * @Description:
 * @Author: ChenLei
 * @Date: 2020/11/20---10:27
 */
public class ViewPagerFragmentActivity extends BaseActivity {
    private List<Fragment> fragmentList;
    private ViewPager vp_main;
    private TextView tvTab1, tvTab2, tvTab3, tvTab4;

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContenView(R.layout.activity_viewpager, R.string.app_name);

        tvTab1 = findViewById(R.id.tv_tab1);
        tvTab2 = findViewById(R.id.tv_tab2);
        tvTab3 = findViewById(R.id.tv_tab3);
        tvTab4 = findViewById(R.id.tv_tab4);

        fragmentList = new ArrayList<>();
        Fragment homeFragment = new HomeFragment();
        Fragment twoFragment = new TwoFragment();
        Fragment threeFragment = new ThreeFragment();
        Fragment fourFragment = new FourFragment();
        fragmentList.add(homeFragment);
        fragmentList.add(twoFragment);
        fragmentList.add(threeFragment);
        fragmentList.add(fourFragment);

        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        vp_main = findViewById(R.id.vp_main);
        vp_main.setAdapter(myPagerAdapter);
        vp_main.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        initSelTabtv(tvTab1);
                        break;
                    case 1:
                        initSelTabtv(tvTab2);
                        break;
                    case 2:
                        initSelTabtv(tvTab3);
                        break;
                    case 3:
                        initSelTabtv(tvTab4);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        initSelTabtv(tvTab1);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(@NonNull FragmentManager fm) {
            super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onButtonClick(View view) {
        switch (view.getId()) {
            case R.id.tv_tab1:
                vp_main.setCurrentItem(0);
                initSelTabtv(tvTab1);
                break;
            case R.id.tv_tab2:
                vp_main.setCurrentItem(1);
                initSelTabtv(tvTab2);
                break;
            case R.id.tv_tab3:
                vp_main.setCurrentItem(2);
                initSelTabtv(tvTab3);
                break;
            case R.id.tv_tab4:
                vp_main.setCurrentItem(3);
                initSelTabtv(tvTab4);
                break;
        }
    }

    private void initTabtv(TextView... tv) {
        for (TextView mtv : tv) {
            mtv.setTextColor(getMColor(R.color.text_color));
            mtv.setBackgroundResource(R.drawable.layer_white_blue_btm);
        }
    }

    private void initSelTabtv(TextView tv) {
        initTabtv(tvTab1, tvTab2, tvTab3, tvTab4);
        tv.setTextColor(getMColor(R.color.blue4d));
        tv.setBackgroundResource(R.drawable.layer_white_gray_btm);
    }

}
